/*
 * ================================================================================变量定义区
 */
/*
 * ================================================================================页面初始化函数
 */
$(function()
{
	//加载导航栏
	loadMenu();
});
/*
 * ================================================================================函数定义区
 * 
 */

/**
 * 异步加载导航栏
 */
function loadMenu()
{	
	$.ajax(
	{
		type:"post",
		url:BASE_PATH+"/menuManagement/loadMenu.do",
		dataType:"json",
		async : false,
		data:
		{	
		},
		success:function(data)
		{   
			var menuData = data;
			var setData = function(){
				var html = "";
				for(var i=0,j=menuData.length;i<j;i++){
					var thisData = menuData[i];
					var type = "";
					if(thisData.params){
						type = thisData.params.type;
					}
					//设置第一级菜单
					if(thisData.level === 0){
						html += '<li class="nav-item" >'+
									'<div class="nav-item-text"  data-menuval="'+thisData.menuVal+'" data-params="'+type+'" data-requesttype="'+thisData.requestType+'" data-opentype="'+thisData.menu_opentype+'" data-type="'+thisData.type+'">'+thisData.menuName+'</div>';
						//获取子菜单
						thisChildHtml = getChildData(thisData.id);
						//拼接子菜单
						html += thisChildHtml;
						//完成该菜单的拼接
						html += '</li>';
					}
				}
				$(".nav").html(html);
			};
			
			var getChildData = function(parentId){
				var flag = false;
				var html = '<ul class="nav-child">';
				for(var i=0,j=menuData.length;i<j;i++){
					var thisData = menuData[i];
					if(thisData.parentId === parentId){
						flag = true;
						var type = "";
						if(thisData.params){
							type = thisData.params.type;
						}
						html += '<li class="nav-child-item" >'+
									'<div class="nav-child-item-text" data-menuval="'+thisData.menuVal+'" data-params="'+type+'" data-requesttype="'+thisData.requestType+'" data-opentype="'+thisData.menu_opentype+'" data-type="'+thisData.type+'">'+thisData.menuName+'</div>';
						var childMenu = getChildData(thisData.id);
						if(childMenu){
							html += childMenu;
						}
						html += '</li>';
					}
				}
				html += '</ul>';
				//如果存在子元素，则返回拼接的字符串，否则返回空字符串
				if(flag){
					return html;
				}
				return "";
			};
			
			setData();
			
			//绑定点击事件
			$(".nav").find("div").click(function(){
				var data = $(this).data();
				if(data.type === 1){
					send(BASE_PATH+data.menuval, data.params, data.requesttype, data.opentype);	
				}else if(data.type === 2){
					eval(data.menuval);
				}
			});
		},
		error:function(e)
		{
			openAlert('操作失败！错误信息：<div>'+e.responseText+"</div>");
		}
		
	});
}


/**
 * 自定义send函数。灵活发送post和get请求
 * @param url 请求路径
 * @param params 请求携带的参数
 * @param sendType 发送请求的格式
 */
function send(url, params,sendType,openType)
{
	var temp = document.createElement("form");
	temp.action = url;
	temp.method = sendType;
	temp.style.display = "none";
	console.log(sendType,openType);
	if(openType == 1){
		 temp.target = "_self"; 
	}else{
		 temp.target = "_blank"; 
	}
	
	var opt = document.createElement("textarea");
	opt.name = "type";
	opt.value = params;
	temp.appendChild(opt);
	document.body.appendChild(temp);
	temp.submit();
	return temp;
}